package bma.common.langutil.ai.callback;

import bma.common.langutil.ai.stack.AIStack;

/**
 * 
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 *
 * @param <CALL_TYPE>
 * @param <RESP_TYPE>
 */
public abstract class AICBConvert<CALL_TYPE, RESP_TYPE> implements
		AICallback<CALL_TYPE> {

	protected AIStack<RESP_TYPE> stack;

	public AICBConvert(AIStack<RESP_TYPE> stack) {
		super();
		this.stack = stack;
	}

	@Override
	public void onSuccess(CALL_TYPE result) {
		try {
			handleResult(result);
		} catch (Exception e) {
			failure(e);
		}
	}

	protected abstract void handleResult(CALL_TYPE result);

	@Override
	public void onFailure(Throwable t) {
		stack.failure(t);
	}

	public boolean success(RESP_TYPE result) {
		return stack.success(result);
	}

	public boolean failure(Throwable t) {
		return stack.failure(t);
	}

}
